package org.uninova.mobis.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * DBUtilsImpl
 * @author PAF
 */
public class DBUtilsImpl {

	/**
	 * DBUtilsImpl Class Constructor
	 */
	public DBUtilsImpl() {}
	
	/**
	 * Starts a {@link java.sql.Connection} to database MobiS_DB
	 * @return The {@link java.sql.Connection} object
	 */
	public Connection startConnection() {
		/*   Access credentials for MS SQL Server - Deprecated  */
		//String username = "paf" ;
		//String password = "grisgris123" ;
		//String url = "jdbc:sqlserver://10.172.67.227:1433;databaseName=MobiS_DB" ;
		
		/*   Access credentials for MySQL Server   */
		String username = "root" ;
		String password = "gris" ;
		String url = "jdbc:mysql://127.0.0.1:3306" ;
		
		try {
			//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url, username, password);
			
			return conn ;
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null ;
	}
	
	/**
	 * Starts a {@link java.sql.Connection} to the database referred on the input parameter <code>url</code>
	 * @param url		The URL for the database connection to start
	 * @param username	The username for the database connection to start
	 * @param password 	The password for the database connection to start
	 * @return			The {@link java.sql.Connection} object
	 */
	public Connection startConnection(String url, String username, String password) {
		try {
			//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url, username, password);
			
			return conn ;
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null ;
	}
	
	/**
	 * Executes a query to the database given by the input parameter <code>conn</code>
	 * @param conn		The {@link java.sql.Connection} object that references the database to query
	 * @param sqlQuery	The query statement
	 * @return			A {@link java.sql.ResultSet} object with the response for the input query
	 */
	public ResultSet queryDB(Connection conn, String sqlQuery) {
		try {	
			ResultSet set = conn.createStatement().executeQuery(sqlQuery) ;
			
			return set ;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null ;
	}
	
	/**
	 * Executes an update on the database given by the input parameter <code>conn</code>
	 * @param conn			The {@link java.sql.Connection} object that references the database to update
	 * @param sqlStatement	The update statement
	 * @return				The exit value for the database update
	 */
	public int modifyDB(Connection conn, String sqlStatement) {
		try {
			return conn.createStatement().executeUpdate(sqlStatement) ;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1 ;
	}
}
